<script type="text/javascript" src="/bootstrap_select/bootstrap-select.js"></script>
<link rel="stylesheet" type="text/css" href="/bootstrap_select/bootstrap-select.css">
<div class="nav-tabs-custom">
    <div class="tab-content">
        <ul class="nav nav-tabs">
            <li class="active">设备列表</li>
        </ul>
        <div class="tab-pane active" id="tab_2">
            <div class="box box-primary">
                <div class="box-header">
                    <form class="form-horizontal" method="get" action="{{ Request::url() }}" pjax-container>
                        <div class="form-group">

                            <div class="col-sm-2">
                                <input type="text" name="device_code" id="deviceCode" value="{{ Request::input('device_code') }}"
                                       class="form-control" placeholder="请输入设备SN号">
                            </div>

                            <div class="col-sm-2">
                                <select name="device_type" id="device_type" class="form-control">
                                    <option value="">请选择设备类型</option>
                                    <option value="1" @if(Request::input('device_type') == 1) selected="selected" @endif>蓝牙动态血糖仪</option>
                                    <option value="2" @if(Request::input('device_type') == 2) selected="selected" @endif>蓝牙指尖血糖仪</option>
                                    <option value="3" @if(Request::input('device_type') == 3) selected="selected" @endif>GPRS指尖血糖仪</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select name="is_bundled" id="is_bundled" class="form-control">
                                    <option value="">请选择绑定状态</option>
                                    <option value="NO_BUND" @if(Request::input('is_bundled') == 'NO_BUND') selected="selected" @endif>未绑定</option>
                                    <option value="1" @if(Request::input('is_bundled') == '1') selected="selected" @endif>已绑定</option>
                                </select>
                            </div>
                            <span class="col-sm-3">
                                <button type="submit" id="auth-button" class="btn btn-primary">查询</button>
                                <button type="button" class="btn btn-default" id="reset" style="margin-left: 20px;">重置</button>
                                <button type="button" id="btn_add" onclick="brand_list()"  class="btn btn-primary btn_bound" style="margin-left: 20px;">添加</button>
                            </span>
                        </div>
                    </form>
                </div>
            </div>
            <!--代理统计表格开始-->
            <div class="box">
                <div class="box-body">
                    <table class="table table-bordered table-hover text-center">
                        <thead>
                        <tr>
                            <th>设备SN号</th>
                            <th>产品型号</th>
                            <th>品牌名称</th>
                            <th>设备类型</th>
                            <th>绑定状态</th>
                            <th>操作</th>

                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($result as $item)
                        <tr>
                            <td>{{$item->device_code}}</td>
                            <td>{{$item->brand_name}}</td>
                            <td>{{$item->facory_name}}</td>
                            <td>
                                @if($item->device_type == '1')
                                    蓝牙动态血糖仪
                                @elseif($item->device_type == '2')
                                    蓝牙指尖血糖仪
                                @else
                                    GPRS指尖血糖仪
                                @endif

                            </td>
                            <td>
                                @if($item->is_bundled == '1')
                                    已绑定
                                @else
                                    未绑定
                                @endif
                            </td>
                            <td>
                                <button type="button" data_id = "{{$item->id}}" onclick="brand_list()"  class="btn btn-success btn_save">编辑</button>
                                <button type="button" class="btn btn-danger" onclick="delete_device({{$item->id}},{{$item->is_bundled}})">删除</button>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $result->appends(['brand_name' => Request::input("brand_name"),'status'=>Request::input("status")])->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

<form action="/admin/Device/addFacory" method="post" accept-charset="UTF-8">
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="border-radius: 10px;margin-top:30%;">
                <div class="modal-header" style="background: #3c8dbc;color: #fff;border-top-left-radius:10px;border-top-right-radius: 10px;">
                    <h4 class="modal-title" id="myModalLabel">新增</h4>
                </div>
                <input type="hidden" name="device_id" class="form-control" id="device_id">

                <div class="modal-body">
                    <div class="form-group">
                        <label for="txt_departmentname">设备SN号：</label>
                        <input type="text" name="device_code" class="form-control" id="device_code" placeholder="请输入">
                    </div>
                    <div class="modal-body">
                        <div class="form-group">
                            <label for="txt_departmentname">产品型号：</label>

                            <select id="id_select" class="selectpicker form-control" data-live-search="true"  data-max-options="1"  style="width: 300px;">

                            </select>
                        </div>
                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                        <button type="submit" id="btn_submit" class="btn btn-primary" data-dismiss="modal"  style="margin-left: 20px;">确 定</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<form method="post" id="form2" >
    <input type="file" name="photo" id="add_img" style="display: none;"/>
</form>

<script type="text/javascript">
    $('#reset').click(function () {
        $('#deviceCode').val('');
        $('#device_type').val('');
        $('#is_bundled').val('');
    });

    $("#btn_add").click(function () {
//        admin_toastr('Message...', 'warning');
        $("#myModalLabel").text("添加设备");
        $('#myModal').modal();
        $('#facory_id').val(0);
    });

    //修改
    $(".btn_save").click(function () {
        $("#myModalLabel").text("编辑设备");
        $('#myModal').modal();
        var id = $(this).attr('data_id');
        //Ajax获取详情
        $.ajax({
            url : 'one_device',
            data:{'id':id},
            success:function(result){
                var data = result.data[0];
                $('#device_code').val(data.device_code);
                $('.filter-option').html(data.brand_name);
                $('#id_select').val(data.fid);
                $('#device_id').val(data.id);

            }
        });
    });

    function brand_list(){
        // 获取未绑定设备
        $.ajax({
            url : 'brand_list',
            type: 'get',
            dataType: 'json',
            data: {},
            success:function(result){
                var data = result.data;
                var html = '';
                for ( var i = 0; i < data.length; i++ ) {
                    html += '<option value="'+ data[i].id +'">'+ data[i].brand_name +'</option>';
                }
                $('#id_select').html(html);
                $('.selectpicker').selectpicker('refresh');
                $('.selectpicker').selectpicker('show');
            }
        });
        $('#btn_submit').attr('data_id',$(this).attr('data_id')); //给确定按钮设置用户id属性

        $("#myModalLabel").text("设备绑定");

        $('#myModal').modal();
    }


//      $(".btn_bound").click(function () {
//
//    });


    //下拉多选搜索框
    $(window).on('load', function () {
        $('.selectpicker').selectpicker({
            'selectedText': 'cat'
        });
    });


    $('#btn_submit').on('click',function (e) {
        var data = {};
        data['device_code'] = $('#device_code').val();

        data['facory_id'] = $('#id_select').val();

        data['id'] = $('#device_id').val();

        if(data['device_code'] == "" || data['facory_id'] == "") {
            alert('缺少参数');return;
        }
        console.log(data);
        $.ajax({
            url :  'add_device',
            type:'post',
            dataType:'json',
            data:data,
            success:function(result){
                if ( result.code != 0 ) {
                    layer.msg('操作失败')

                }else{

                    layer.msg('操作成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });

                }
            }
        });
    })

    function delete_device(id,is_bundled){
        if(is_bundled == 1){
            layer.msg('设备已绑定，不支持删除');return
        }

        $.ajax({
            url :  'delete_device',
            type:'post',
            dataType:'json',
            data: {
                id: id,
            },
            success:function(result){
                console.log(result);
                if ( result.code != 0 ) {
                    layer.msg(result.msg)

                }else{

                    layer.msg('删除成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });

                }
            }
        });
    }


</script>
